package org.pp.core;


//import com.auth0.jwt.exceptions.JWTDecodeException;
//import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
//import org.springframework.web.servlet.resource.NoResourceFoundException;

import java.util.Iterator;

@ControllerAdvice
@Slf4j
public class CommonExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result<Object> handle(Exception e) {
        log.error(e.getMessage(), e);
        return Result.fail("系统错误");
    }

    @ExceptionHandler(value = RedisConnectionFailureException.class)
    @ResponseBody
    public Result<Object> handle(RedisConnectionFailureException e) {
        log.error(e.getMessage(), e);
        return Result.fail("redis连接异常");
    }



    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseBody
    public Result<Object> handle(MethodArgumentNotValidException e) {
        log.error(e.getMessage(), e);
        StringBuffer errorMsg = new StringBuffer();
        for (Iterator<FieldError> iterator = e.getBindingResult().getFieldErrors().iterator(); iterator.hasNext(); ) {
            String erro = iterator.next().getDefaultMessage();
            errorMsg.append(erro);
            if (iterator.hasNext()) {
                errorMsg.append(",");
            }
        }
        return Result.fail( errorMsg.toString());
    }


    @ExceptionHandler(value = BindException.class)
    @ResponseBody
    public Result<Object> handle(BindException e) {
        log.error(e.getMessage(), e);
        return Result.fail("参数格式错误");
    }

    /*@ExceptionHandler(value = NoResourceFoundException.class)
    @ResponseBody
    public Result<Object> handle(NoResourceFoundException e) {
        log.error(e.getMessage(), e);
        return Result.fail("接口地址错误");
    }*/

    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    @ResponseBody
    public Result<Object> handle(HttpMessageNotReadableException e) {
        log.error(e.getMessage(), e);
        return Result.fail("参数格式错误");
    }

    @ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
    @ResponseBody
    public Result<Object> handle(MethodArgumentTypeMismatchException e) {
        log.error(e.getMessage(), e);
        return Result.fail("参数格式错误");
    }


    @ExceptionHandler(value = IllegalStateException.class)
    @ResponseBody
    public Result<Object> handle(IllegalStateException e) {
        log.error(e.getMessage(), e);
        return Result.fail("参数错误");
    }

    @ExceptionHandler(value = MissingServletRequestPartException.class)
    @ResponseBody
    public Result<Object> handle(MissingServletRequestPartException e) {
        log.error(e.getMessage(), e);
        return Result.fail("文件参数错误");
    }


    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public Result<Object> handle(HttpRequestMethodNotSupportedException e) {
        log.error(e.getMessage(), e);
        return Result.fail("请求方式错误，请切换请求方式");
    }

    @ExceptionHandler(value = BizExcept.class)
    @ResponseBody
    public Result<Object> handle(BizExcept e) {
        return Result.fail(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(value = JWTDecodeException.class)
    @ResponseBody
    public Result<Object> handle(JWTDecodeException e) {
        return Result.fail(BizErro.TOKEN_EXPIRE);
    }

    @ExceptionHandler(value = TokenExpiredException.class)
    @ResponseBody
    public Result<Object> handle(TokenExpiredException e) {
        return Result.fail(BizErro.TOKEN_EXPIRE);
    }
}
